package com.voltmao.controller.module.sys;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.voltmao.webpro.frame.analyze.DTOSmith;
import com.voltmao.webpro.frame.dto.ApiResult;
import com.voltmao.webpro.frame.dto.status.Constants;
import com.voltmao.webpro.utils.file.Base64toFileUtil;
import com.voltmao.webpro.utils.file.FileUtils;
import com.voltmao.webpro.utils.http.PostUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * 文件控制层
 *
 * @author zhanglin
 * @date 2018/04/10
 */
@Controller
@RequestMapping(value = "/module/sys/file")
public class FileController {

    @ResponseBody
    @RequestMapping(value = "/test.json", method = RequestMethod.POST)
    public ApiResult authLogin(HttpServletRequest request) {
        return new DTOSmith(request).builderApiResult((request1, apiResult)->{
            String path = FileUtils.getWebInfPath();
            String filePath = path+"/upload/";
            System.out.println(filePath);
            String file = filePath+"test";
            try {
                FileUtils.createDir(filePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Base64toFileUtil.base64StringToPDF(Base64toFileUtil.base64, file);
            apiResult.setStatus(Constants.SUCCESS);
            return null;
        });
    }

    @ResponseBody
    @RequestMapping(value = "/testPost.json", method = RequestMethod.POST)
    public ApiResult filePost(HttpServletRequest request) {
        return new DTOSmith(request).builderApiResult((request1, apiResult)->{
            String path = FileUtils.getWebInfPath();
            String filePath = path+"/upload/";
            String file = filePath+"test.pdf";

            try {
                File myFile = new File(file);
                if(myFile.exists()){
                    System.out.println("发送文件出去");
                    String out = PostUtil.sendPostWithFile("http://192.168.1.126:3000/upload/files", myFile);
                    System.out.println("发送文件结果："+out);
                } else{
                    System.out.println("文件不存在");
                }
            } catch (Exception e){
                e.printStackTrace();
            }
            apiResult.setStatus(Constants.SUCCESS);
            return null;
        });
    }

}
